﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms.Integration;


using BL.Entity;
using BL.Service;
using Microsoft.Reporting.WinForms;
using System.Drawing.Printing;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Transactions;
using System.Reflection;
namespace LTO
{
    /// <summary>
    /// Interaction logic for ReportApp.xaml
    /// </summary>
    public partial class ReportApp : Window
    {
        List<NumberDTO> numberDTO = new List<NumberDTO>();
        Font UseFont = new Font("MS Sans Serif", 12);
        System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
        System.Drawing.SolidBrush myBrushGray = new System.Drawing.SolidBrush(System.Drawing.Color.Gray);

        System.Drawing.Pen blackPen = new System.Drawing.Pen(System.Drawing.Color.Black, 1);

        List<ShowNumberDTO> show;
        tbl_NumberServer Numberservice = new tbl_NumberServer();
        tbl_PayService PayService = new tbl_PayService();
        Pay limit;
        List<ShowNumberDTO> datasource;
        string headerTxt;
        string NumberType;
        MessageBoxResult deleteFag;
        List<ShowNumberDTO> printDataSource;

        public ReportApp(List<ShowNumberDTO> datasourceNum, string numType, decimal LimitBuy, MessageBoxResult IsdeletNum)
        {
            InitializeComponent();
            NumberType = numType;
            deleteFag = IsdeletNum;
            printDataSource = datasourceNum;

            //AutoPrint(datasource, NumberType, LimitBuy);
        }

        private void Print()
        {
            switch (NumberType)
            {
                case ("O3"):
                    headerTxt = "3 ตัวบน";
                    break;
                case ("U3"): headerTxt = "3 ตัวล่าง";
                    break;
                case ("T3"): headerTxt = "โต๊ด 3 บน";
                    break;
                case ("O2"): headerTxt = "2 ตัวบน";
                    break;
                case ("U2"): headerTxt = "2 ตัวล่าง";
                    break;
                case ("T2"): headerTxt = "โต๊ด 2 บน";
                    break;
                case ("O1"): headerTxt = "วิ่งบน";
                    break;
                case ("O1U"): headerTxt = "บน หลักหน่วย";
                    break;
                case ("O1D"): headerTxt = "บน หลักสิบ";
                    break;
                case ("O1H"): headerTxt = "บน หลักร้อย";
                    break;
                case ("U1"): headerTxt = "วิ่งล่าง";
                    break;
                case ("U1D"): headerTxt = "ล่าง หลักสิบ";
                    break;
                case ("U1U"): headerTxt = "ล่าง หลักหน่วย";
                    break;
                case ("TF"): headerTxt = "คู่หน้า";
                    break;
                case ("TS"): headerTxt = "คู่ถ่าง";
                    break;
                case ("FN"): headerTxt = "พวง 4 ตัว";
                    break;
                case ("FIN"): headerTxt = "5 บนโต๊ด";
                    break;

            }


            if (deleteFag == MessageBoxResult.Yes)
            {
                List<ShowNumberDTO> delteNum = new List<ShowNumberDTO>();
                delteNum = printDataSource.Where(w => w.Price > 0).ToList();
                tbl_NumberServer numSvr = new tbl_NumberServer();
                NumberDTO numselect = new NumberDTO();
                foreach (ShowNumberDTO del in delteNum)
                {
                    decimal dele = del.Price;

                    Numberservice.Addnumber(del.Number, dele, 0, 0, Gobalsource.UserNameLogin, NumberType, true);
                }
              

            }
            datasource = new List<ShowNumberDTO>();
            datasource = printDataSource;
            PrintDocument printDoc = new PrintDocument();
            PaperSize ps = new PaperSize();
            ps.RawKind = (int)PaperKind.A4;
            printDoc.DefaultPageSettings.PaperSize = ps;
            printDoc.DefaultPageSettings.Landscape = false;

            printDoc.PrinterSettings.PrinterName = System.Configuration.ConfigurationManager.AppSettings["Printname"];
            printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

            printDoc.Print();
        }
        public ReportApp()
        {
            InitializeComponent();

        }

        int checkPageNext = 0;
        int countIndex = 0;
        int pageCount = -1;

        private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            int recX = 149;
            int topPosition = SystemInformation.WorkingArea.X;
            int leftPosition = 30;
            int recH = 440;
            int lineH = 22;
            int txtPad = 17;
            if (datasource[0].Number.Length > 3)
            {
                txtPad = 10;
            }
            string summaryFooter = String.Format("{0:#,#}", datasource.Sum(s => s.Price));
            int[] Toppositoin = new int[] { 70, 590 };
            System.Drawing.Rectangle rec1;
            System.Drawing.Rectangle rec2;
            System.Drawing.Rectangle rec3;
            System.Drawing.Rectangle rec4;
            System.Drawing.Rectangle rec5;









            int pageTop = 45;
            int footerPosition = 0;
            for (int p = 1; p <= 2; p++)
            {
                if (countIndex < datasource.Count)
                {
                    footerPosition = (460 * (p - 1));
                    pageCount += 1;
                    e.Graphics.DrawString(string.Format("ยอดขายรวมแต่ละตัวของ {0}", headerTxt), UseFont, myBrush, 200, pageTop);
                    e.Graphics.DrawString(string.Format("หน้าที่ {0}", pageCount), UseFont, myBrush, 725, pageTop);

                    topPosition = Toppositoin[p - 1];
                    rec1 = new System.Drawing.Rectangle(leftPosition, topPosition, recX, recH);
                    rec2 = new System.Drawing.Rectangle(leftPosition, topPosition, recX * 2, recH);
                    rec3 = new System.Drawing.Rectangle(leftPosition, topPosition, recX * 3, recH);
                    rec4 = new System.Drawing.Rectangle(leftPosition, topPosition, recX * 4, recH);
                    rec5 = new System.Drawing.Rectangle(leftPosition, topPosition, recX * 5, recH);

                    e.Graphics.DrawRectangle(blackPen, rec1);
                    e.Graphics.DrawRectangle(blackPen, rec2);
                    e.Graphics.DrawRectangle(blackPen, rec3);
                    e.Graphics.DrawRectangle(blackPen, rec4);
                    e.Graphics.DrawRectangle(blackPen, rec5);

                    for (int l = 0; l < 5; l++)
                    {
                        for (int i = 0; i < 20; i++)
                        {
                            if (countIndex < datasource.Count)
                            {

                                decimal price = Convert.ToDecimal(datasource[countIndex].PriceShow);

                                string num = datasource[countIndex].Number;
                                if (price > 0)
                                {
                                    e.Graphics.DrawString(string.Format("{0} = {1}", num.ToString().PadLeft(5, ' '), datasource[countIndex].PriceShow.ToString().PadLeft(txtPad, ' ')), UseFont, myBrush, rec1.X + (recX * l), rec1.Y + (i * lineH));
                                }
                                else
                                {
                                    e.Graphics.DrawString(string.Format("{0} = {1}", num.ToString().PadLeft(5, ' '), datasource[countIndex].PriceShow.ToString().PadLeft(txtPad, ' ')), UseFont, myBrushGray, rec1.X + (recX * l), rec1.Y + (i * lineH));
                                }
                                countIndex += 1;
                            }

                        }
                    }
                    pageTop += 520;
                }
                else
                {


                    break;
                }
            }
            checkPageNext += 200;

            if (checkPageNext < datasource.Count)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.Graphics.DrawString(string.Format("ยอดส่วนเกิน ={0}", summaryFooter.ToString().PadLeft(10, ' ')), UseFont, myBrush, 600, Toppositoin[1] + footerPosition);
                e.HasMorePages = false;

            }



        }

        private void AutoPrint(List<ShowNumberDTO> datasource, string NumberType, decimal LimitBuy)
        {
            List<ReportNumDTO> numberResult = new List<ReportNumDTO>();
            numberResult.AddRange(SetReport(datasource, LimitBuy, NumberType));

            Demo printAuto = new Demo(numberResult);
            printAuto.Run();
            printAuto.Dispose();
            // PreparReport(numberResult);
        }

        #region :: Print Old Solution ::
        private void PreparReport(List<ReportNumDTO> numberResult)
        {
            // Getnum();
            //Merchant is the class used to enter new product names and their prices.
            WindowsFormsHost host = new WindowsFormsHost();
            ReportViewer reportViewer = new ReportViewer();
            //Specifying local processing mode for the ReportViewer. 
            reportViewer.ProcessingMode = ProcessingMode.Local;
            //Specifying the location of the report definition file. 

            reportViewer.LocalReport.ReportPath = System.Configuration.ConfigurationManager.AppSettings["ReportPath"];
            //Creating a new ReportDataSource with the name of the DataSource and the object // which is to be used as the DataSource. 
            ReportDataSource ds = new ReportDataSource("DataSet1", numberResult);
            //Adding the ReportDataSource to the DataSoure of the ReportViewer 
            reportViewer.LocalReport.DataSources.Add(ds);
            //Causes the current report in the Report Viewer to be processed and rendered. 
            reportViewer.RefreshReport();
            //Sets the child control hosted by the WindowsFormsHost element. 
            host.Child = reportViewer;
            //Add the WindowsFormsHost element to the Grid in the Window1.xaml 
            reportGrid.Children.Add(host);
        }

        protected List<ReportNumDTO> SetReport(List<ShowNumberDTO> numberDTO, decimal limit, string numberType)
        {
            List<ReportNumDTO> numberResult = new List<ReportNumDTO>();
            List<ReportNumDTO> numberResult2 = new List<ReportNumDTO>();
            decimal overSummary = numberDTO.Where(s => s.Price > limit).ToList().Sum(s => s.Price - limit);
            int Pageindex = 0;
            int pageCount = 1;

            foreach (ShowNumberDTO item in numberDTO)
            {

                ReportNumDTO data = new ReportNumDTO();
                data.Number = item.Number;
                data.NumType = numberType;
                data.Price = item.Price;
                data.OverCut = limit;
                data.CutOutSummary = overSummary;


                if (pageCount == 100)
                {
                    Pageindex += 1;
                    pageCount = 1;

                }
                else
                {
                    pageCount += 1;
                }

                data.Page = Pageindex;


                numberResult.Add(data);

            }
            int page1Start = 0;
            int page2Start = 20;
            int page3Start = 40;
            int page4Start = 60;
            int page5Start = 80;
            int counter = 1;


            for (int i = 0; i < numberResult.Count(); i += 1)
            {


                ReportNumDTO data = new ReportNumDTO();

                data.NumType = numberType;

                data.OverCut = limit;
                data.CutOutSummary = overSummary;
                data.Page = numberResult[i].Page;
                if (numberResult.Count() > page1Start)
                {
                    data.ShowNumColumn1 = string.Format("{0}={1}", numberResult[page1Start].Number, numberResult[page1Start].Price.ToString().PadLeft(10, ' ')); ;
                }
                if (numberResult.Count() > page2Start)
                {
                    data.ShowNumColumn2 = string.Format("{0}={1}", numberResult[page2Start].Number, numberResult[page2Start].Price.ToString().PadLeft(10, ' ')); ;
                }
                if (numberResult.Count() > page3Start)
                {
                    data.ShowNumColumn3 = string.Format("{0}={1}", numberResult[page3Start].Number, numberResult[page3Start].Price.ToString().PadLeft(10, ' ')); ;
                }
                if (numberResult.Count() > page4Start)
                {
                    data.ShowNumColumn4 = string.Format("{0}={1}", numberResult[page4Start].Number, numberResult[page4Start].Price.ToString().PadLeft(10, ' ')); ;
                }
                if (numberResult.Count() > page5Start)
                {
                    data.ShowNumColumn5 = string.Format("{0}={1}", numberResult[page5Start].Number, numberResult[page5Start].Price.ToString().PadLeft(10, ' ')); ;

                }

                if (counter == 20)
                {
                    page1Start += 81;
                    page2Start += 81;
                    page3Start += 81;
                    page4Start += 81;
                    page5Start += 81;
                    counter = 1;
                }
                else
                {
                    page1Start += 1;
                    page2Start += 1;
                    page3Start += 1;
                    page4Start += 1;
                    page5Start += 1;
                    counter += 1;
                }
                numberResult2.Add(data);

            }
            return numberResult2;
        }

        #endregion

        private void Window_Closed(object sender, EventArgs e)
        {

        }

        private void Window_Activated(object sender, EventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Print();
            this.Close();
        }

    }
}
